<template>
  <!--  #ifdef  APP-PLUS || H5 -->
  <component
    :is="AsyncComp"
    v-bind="{ ...$attrs, ...componentProps }"
    :modelValue="componentProps.curBindVal"
    @update:modelValue="componentProps.curBindVal = $event"
  ></component>
  <!--  #endif -->
  <!--  #ifndef  APP-PLUS || H5 -->
  小程序不支持动态组件:编译到小程序时,此处会通过if-else自动代码生成
  <!--  #endif -->
</template>
<script>
export default {
  inheritAttrs: false,
  name: "GdHackDynmicComp",
};
</script>
<script setup>
// #ifdef  APP-PLUS || H5
import {
  defineAsyncComponent,
  defineComponent,
  shallowRef,
  watch,
  computed,
  useAttrs,
  getCurrentInstance,
} from "vue";
const props = defineProps({
  componentFilePath: {
    type: String,
  },
  rootPathType: {
    type: String,
    default: "customize",
    validator(prop) {
      return ["customize", "pages"].includes(prop);
    },
  },
  componentProps: {
    type: Object,
    default: () => ({}),
  },
});
const dynmicImportCustomizeSafed = (componentFilePath) => {
  if (typeof componentFilePath !== "string") {
    return PromiseRejectionEvent.resolve(false);
  }
  const componentFilePath_withoutExt = componentFilePath.endsWith(".vue")
    ? componentFilePath.replace(".vue", "")
    : componentFilePath;

  let modules = import.meta.glob(
    "../../../static/components/customize/**/*.vue"
  );

  return modules[
    `../../../static/components/customize/${componentFilePath_withoutExt}.vue`
  ]();
};
const dynmicImportPageSafed = (componentFilePath) => {
  if (typeof componentFilePath !== "string") {
    return PromiseRejectionEvent.resolve(false);
  }
  const componentFilePath_withoutExt = componentFilePath.endsWith(".vue")
    ? componentFilePath.replace(".vue", "")
    : componentFilePath;

  let modules = import.meta.glob("../../../pages/**/customize-*.vue");
  return modules[`../../../pages/${componentFilePath_withoutExt}.vue`]();
};
const AsyncComp = computed(() => {
  if (typeof props.componentFilePath !== "string") {
    return PromiseRejectionEvent.resolve(false);
  }
  return defineAsyncComponent(() => {
    if (props.rootPathType === "customize") {
      return dynmicImportCustomizeSafed(props.componentFilePath);
    } else if (props.rootPathType === "pages") {
      return dynmicImportPageSafed(props.componentFilePath);
    }
  });
});
// #endif
</script>
